﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HuntWump2
{
    class GameControl
    {
        private static char triviatype;
        public static void gameControl()
        {
            //instantiate all new objects
            Map map = new Map();
            PlayerObject player = new PlayerObject();
            Wumpus wumpus = new Wumpus();
            TriviaStubsFinal trivia = new TriviaStubsFinal();

            //display main menu
            //anything clicked will call the appropriate method from form1 (not in gameControl)

        }//end main method

        //win game when shoot wumpus only
        //lose game when wumpus beats you in a fight, you run out of coins

        public static void newGame(String name)
        {
            //then "start new game" directs here

            //Ask for name, give to player through menu
            //All new variables set by map/player in gameControl method above
            PlayerObject.userName = name;

            //Display cave
            gwee.doorz(Cave.connectedRoomsDirection(Map.playerRoomNumber));
            gwee.updatestats(HuntWump2.PlayerObject.numOfTurns, PlayerObject.numOfArrows, PlayerObject.numOfCoins, Map.playerRoomNumber); //turns, arrows, coins, room #
        }
        //for turns following non-moving turns

        //BUTTON CLICKED = START OF NEW TURN

        public static void turn(char c)
        {
            PlayerObject.numOfTurns++;
            bool finishTurn = true;
            if (PlayerObject.numOfCoins < 1 || PlayerObject.numOfArrows < 1)
            {
                endGame(false);
            }

            triviatype = c;

            if (c == 'h')
            {
                //warnings
                int[] hazards = gweeHazards();

                //hazards and secrets
                // -1 indicates no hazards
                if (Map.hazardInARoom(Map.playerRoomNumber) == 2)
                {
                    //secret in room
                    //call trivia/player to display secret
                } else if (Map.hazardInARoom(Map.playerRoomNumber) == 1)
                {
                    //bat in room
                    //automatically move to different room
                    Map.moveBat();
                    //redisplay screen from gwee
                    gwee.doorz(Cave.connectedRoomsDirection(Map.playerRoomNumber));
                    gwee.updatestats(HuntWump2.PlayerObject.numOfTurns, PlayerObject.numOfArrows, PlayerObject.numOfCoins, Map.playerRoomNumber);
                    finishTurn = false;
                } else if (Map.hazardInARoom(Map.playerRoomNumber) == 0)
                {
                    //pit in room
                    //must answer 2/3 trivia questions correctly, otherwise end game
                }

                if (finishTurn == true)
                {
                    //check for presence of wumpus
                    if (Map.wumpusInPlayerRoom() == true)
                    {
                        //must answer 3 of 5 trivia questions correctly
                        //otherwise it's game over
                        //then wumpus runs away
                    }
                }
                else
                {
                    //this is the turn that happens after a user escapes a pit or is carried by a bat
                    turn('h');
                }
            }
            else if (c == 'a')
            {
                //buying arrows
                //takes 2 coins, must get 2+/3 right in order  to get 2 arrows 
                if (PlayerObject.numOfCoins < 2)
                {

                    MessageBox.Show("Sorry, you do not have enough coins.");
                }
                else
                {
                    PlayerObject.numOfCoins -= 2;
                    Trivia trivia = new Trivia();
                    trivia.Show();
                }
            }
            else if (c == 's')
            {
                //buying secret
                //1 secret costs 1 coin, must get 2/3 right to get it
                if (PlayerObject.numOfCoins < 1)
                {

                    MessageBox.Show("Sorry, you do not have enough coins.");
                }
                else
                {
                    PlayerObject.numOfCoins -= 1;
                    Trivia trivia = new Trivia();
                    trivia.Show();
                }

            }
            else if (c == 'f')
            {
                //firing arrow
                Form1.i = -1;
            }
        }

        public static void continueTurn()
        {
            if (Trivia.answerSetCorrect)
            {
                if (triviatype == 'a')
                {
                    PlayerObject.numOfArrows++;
                }
                else if (triviatype == 's')
                {
                    //display secret
                    MessageBox.Show(TriviaStubsFinal.getSecret());
                }
            }
            gwee.updatestats(PlayerObject.numOfTurns, PlayerObject.numOfArrows, PlayerObject.numOfCoins, Map.playerRoomNumber);
        }
        //for turns after moving to a new room - by choice
        public static void turn(int direction)
        {
            bool finishTurn = true;
            if (PlayerObject.numOfArrows < 1 || PlayerObject.numOfCoins < 1)
            {
                endGame(false);
            }
            //true indicates the turn should proceed as normal, false: turn is terminated early
            //win game when shoot wumpus only -> call endTurn
            //lose game when wumpus beats you in a fight, you run out of coins

            move(direction);
            if (PlayerObject.numOfCoinsCollected < 100)
            {
                PlayerObject.numOfCoins++;
                PlayerObject.numOfCoinsCollected++;
            }
            PlayerObject.numOfTurns++;
            gwee.updatestats(PlayerObject.numOfTurns, PlayerObject.numOfArrows, PlayerObject.numOfCoins, Map.playerRoomNumber); //turns, arrows, coins, room #
            
            //warnings
            int[] hazards = gweeHazards();
            gwee.updatehazards(hazards);

            //show a piece of trivia
            //from triviastubs

            //hazards and secrets
            // -1 indicates no hazards
            if (Map.hazardInARoom(Map.playerRoomNumber) == 2)
            {
                MessageBox.Show(TriviaStubsFinal.getSecret());
            } else if (Map.hazardInARoom(Map.playerRoomNumber) == 1)
            {
                //bat in room
                //automatically move to different room
                Map.moveBat();
                //redisplay screen from gwee
                gwee.doorz(Cave.connectedRoomsDirection(Map.playerRoomNumber));
                gwee.updatestats(HuntWump2.PlayerObject.numOfTurns, PlayerObject.numOfArrows, PlayerObject.numOfCoins, Map.playerRoomNumber);
                finishTurn = false;
            } else if (Map.hazardInARoom(Map.playerRoomNumber) == 0)
            {
                //pit in room
                //must answer 2/3 trivia questions correctly, otherwise end game
            }

            if(finishTurn == true)
            {
                //check for presence of wumpus
                if (Map.wumpusInPlayerRoom() == true)
                {
                    //must answer 3 of 5 trivia questions correctly
                    //otherwise it's game over
                    //then wumpus runs away
                }
            } else             
            {
                //this is the turn that happens after a user escapes a pit or is carried by a bat
                turn('h');
            }
        }//end turn

        public static void move(int doorNum)
        {
            //get door # from button
            //set room number
            int r = Cave.getRoomInDirection(Map.playerRoomNumber, doorNum);
            Map.playerRoomNumber = r;
            gwee.doorz(Cave.connectedRoomsDirection(r));
        }

       

       

        public static void fireArrow(int i)
        {
            //need to know direction and if wumpus is in that room
            PlayerObject.numOfTurns++;
            PlayerObject.numOfArrows--;
            gwee.updatestats(PlayerObject.numOfTurns, PlayerObject.numOfArrows, PlayerObject.numOfCoins, Map.playerRoomNumber);
            if (Cave.getRoomInDirection(Map.playerRoomNumber, i) == Map.wumpusRoomNumber)
            {
                //you've killed the wumpus
                endGame(true);
            }
            else
            {
                Form1.i = 0;
            }
        }
        public static int[] gweeHazards()
        {
            int[] hazards = new int[3];
            
            if (Map.hazardInARoom(Map.playerRoomNumber) == 1)
            {
                //bat in room
                hazards[0] = 2;
            }
            else if (Map.hazardInARoom(Map.playerRoomNumber) == 0)
            {
                //pit in room
                hazards[1] = 2;
            }
            if (Map.wumpusInPlayerRoom() == true)
            {
                //wumpus in room
                hazards[2] = 2;
            }

            for (int i = 0; i < Map.warningsToPlayer().Count; i++)
            {
                char hazard = Map.warningsToPlayer()[i][0];
                if (hazard.CompareTo('P') == 0)
                {
                    //pit is in adjacent room
                    hazards[1] = 1;
                }
                if (hazard.CompareTo('B') == 0)
                {
                    //bat is in adjacent room
                    hazards[0] = 1;
                }
                if (hazard.CompareTo('W') == 0)
                {
                    //wumpus is in adjacent room
                    hazards[2] = 1;
                }
            }
            return hazards;
        }
        
        
        public static void endGame(Boolean killed)
        {
          //player calculates final score, passed to high score (maybe added), high scores displayed

            if (killed == true)
            {
                MessageBox.Show("Congratualations! You have proven yourself to be more intelligent and capable than the Wumpus. \n Prepare to be hunted. . .");
            }
            else
            {
                MessageBox.Show("You have escaped the testing facility and gone to a better place.");
            }
            if(HighScore.addHighScore(PlayerObject.userName, PlayerObject.KeepScore(PlayerObject.numOfTurns, PlayerObject.numOfCoins, PlayerObject.numOfArrows)) == 0)
            {
                MessageBox.Show("New High Score!");
            }
            else
            {
                MessageBox.Show("Try again for a High Score!");
            }


              
            //happens if player chooses to quit from inside game or if game ends (out of turns or wumpus caught)
            //killed = true -> wumpus killed, otherwise, player loses
        }//end endGame 
        
    }
}
